﻿Public Partial Class TugBeforeAndAfter
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            If Not (Session("TugEvent") Is Nothing) Then
                Dim dt As DataTable
                If (Session("TugEvent") & "" = "Before") Then
                    dt = CreateTugBefore_After()
                    lbLegend.Text = "Tug Boat Before"
                    If Not (Session("TugValue") Is Nothing) Then
                        Dim str() As String = Session("TugValue").ToString().Split(",")
                        For i As Integer = 0 To str.Length - 1
                            If (str(i).Trim() <> "") Then
                                Dim dr As DataRow = dt.NewRow
                                dr.Item("TugName") = str(i)
                                dt.Rows.Add(dr)
                                dt.AcceptChanges()
                            End If
                        Next
                    End If
                    Session("TugBeforeLoading") = dt
                Else
                    lbLegend.Text = "Tug Boat After"
                    dt = CreateTugBefore_After()
                    If Not (Session("TugValue") Is Nothing) Then
                        Dim str() As String = Session("TugValue").ToString().Split(",")
                        For i As Integer = 0 To str.Length - 1
                            If (str(i).Trim() <> "") Then
                                Dim dr As DataRow = dt.NewRow
                                dr.Item("TugName") = str(i)
                                dt.Rows.Add(dr)
                                dt.AcceptChanges()
                            End If

                        Next
                    End If
                    Session("TugAfterLoading") = dt
                End If
                gridTug.DataSource = dt
                gridTug.DataBind()
            End If
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Session("TugEvent") = Nothing
        Session("TugValue") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalPopupTug('N');", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Session("TugEvent") = Nothing
        Session("TugValue") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalPopupTug('S');", True)
    End Sub

    Private Sub btnTug_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTug.Click

        Dim dt As DataTable
        If (Session("TugEvent") & "" = "Before") Then
            If (Session("TugBeforeLoading") Is Nothing) Then
                Session("TugBeforeLoading") = CreateTugBefore_After()
            End If
            dt = Session("TugBeforeLoading")
        Else
            If (Session("TugAfterLoading") Is Nothing) Then
                Session("TugAfterLoading") = CreateTugBefore_After()
            End If
            dt = Session("TugAfterLoading")
        End If
        For Each dm As DataRow In dt.Rows
            If (dm.Item("TugName") = txtTug.Text) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('พบข้อมูลเรือทักได้ถูกทำการเพิ่มก่อนหน้านี้แล้ว');", True)
                Exit Sub
            End If
        Next
        Dim dr As DataRow = dt.NewRow
        dr.Item("TugName") = txtTug.Text
        dt.Rows.Add(dr)
        dt.AcceptChanges()
        If (Session("TugEvent") & "" = "Before") Then
            Session("TugBeforeLoading") = dt
        Else
            Session("TugAfterLoading") = dt
        End If
        gridTug.DataSource = dt
        gridTug.DataBind()
        txtTug.Text = String.Empty
    End Sub

    Private Function CreateTugBefore_After()
        Dim dt As New DataTable
        dt.Columns.Add("TugName", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub gridTug_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTug.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (e.CommandName = "cmddelete") Then
            Dim dt As DataTable
            If (Session("TugEvent") & "" = "Before") Then
                dt = Session("TugBeforeLoading")
            Else
                dt = Session("TugAfterLoading")
            End If
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            If (Session("TugEvent") & "" = "Before") Then
                Session("TugBeforeLoading") = dt
            Else
                Session("TugAfterLoading") = dt
            End If
            gridTug.DataSource = dt
            gridTug.DataBind()
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class